package com.oj.common;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 管坤坤
 * @Description:
 */
public class FileUtil {

    // 负责把filePath对应的文件的内容读取出来，放到返回值里
    public  static  String readFile(String filePath) throws FileNotFoundException {
        StringBuilder result=new StringBuilder ();
        try(FileReader fileReader=new FileReader (filePath)){
            while (true){
                int ch=fileReader.read ();
                if(ch==-1){
                    break;
                }
                result.append ((char)ch);
            }
        } catch (IOException e) {
            e.printStackTrace ();
        }
        return  result.toString ();
    }
    //负责把conten写入到filepath对应的文件中
    public  static  void  writeFile(String filePath,String content)  {
        try (FileWriter fileWriter=new FileWriter (filePath)){
            fileWriter.write (content);
        }catch (IOException e){
            e.printStackTrace ();
        }
    }
    public static void main(String[] args) throws FileNotFoundException {
        FileUtil.writeFile("d:/test.txt", "hello");
        String content = FileUtil.readFile("d:/test.txt");
        System.out.println(content);
    }
}
